﻿using Newtonsoft.Json;

using RestSharp;
using SMSSendTest.Dto;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http.Headers;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace SMSSendTest
{
    public class GannanWaterService
    {

        /**
   * 生成签名sign
   * 加密前：appId=wx123456789&timestamp=1583332804914&key=7214fefff0cf47d7950cb2fc3b5d670a
   * 加密后：E2B30D3A5DA59959FA98236944A7D9CA
   */
        public static string CreateSign(SortedDictionary<string, string> parameters, string key)
        {
            StringBuilder sb = new StringBuilder();
            foreach (KeyValuePair<string, string> entry in parameters)
            {
                string k = entry.Key;
                string v = entry.Value;
                if (!string.IsNullOrEmpty(v) && k != "sign" && k != "key")
                {
                    sb.Append(k + "=" + v + "&");
                }
            }
            sb.Append("key=").Append(key);

            string sign = ComputeMD5Hash(sb.ToString()).ToUpper();
            return sign;
        }
        private static string ComputeMD5Hash(string input)
        {
            using (MD5 md5 = MD5.Create())
            {
                byte[] inputBytes = Encoding.UTF8.GetBytes(input);
                byte[] hashBytes = md5.ComputeHash(inputBytes);

                StringBuilder sb = new StringBuilder();
                foreach (byte b in hashBytes)
                {
                    sb.Append(b.ToString("X2"));
                }
                return sb.ToString();
            }
        }          

        public static string GannanWater()
        {
            try
            {
                var yestoday = DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd");
                string appSecret = Guid.NewGuid().ToString("N"); //"8f785bfde4889bdb8057e616ce819d33dc030ce0";
                appSecret = "8f785bfde4889bdb8057e616ce819d33dc030ce0";
                string appId = "FBD8zvIQ";
                string timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds().ToString();

                Console.WriteLine($"timestamp：{timestamp}");

                SortedDictionary<string, string> sortedMap = new SortedDictionary<string, string>
        {
            { "appId", appId },
            { "timestamp", timestamp }
        };

                string sign = CreateSign(sortedMap, appSecret);
                Console.WriteLine($"签名：{sign}");


                var apiUrl = "http://gannanwater.com:8000/water/buyWaterInfo/outlist";

                
                var jsonData = new
                {

                    beginTime = "2024-12-01",
                    endTime = "2024-12-30"

                };
                var client = new RestClient(apiUrl);

                var request = new RestRequest("", Method.Post);
                request.AddHeader("Content-Type", "application/json");

                request.AddHeader("sign", sign);
                request.AddHeader("appId", appId);
                request.AddHeader("timestamp", timestamp);

           //     request.AddParameter("application/json", jsonData, ParameterType.RequestBody);
                request.AddJsonBody(jsonData);

                var key = "392A4147A2A71289D67D7CDFD3833953";
                var customerName = "eebc857512567eb1a6a515e2d1b6b37a58cad6727bb8a59253b4ffe538048881";
                Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);

                var mimi = Encoding.GetEncoding("GBK").GetString(OSharp.Encrypt.SMEncrypt.ECBDecrypt(customerName, key));


                var reSend = client.Execute(request);
                if(reSend.IsSuccessful)
                {
                    Console.WriteLine("成功");
                }

                var content =  reSend.Content;
                var obj = JsonConvert.DeserializeObject<GannanWater>(content);
                Console.WriteLine(  obj.msg);
               
           

                 
     

                if (obj.data!=null&&obj.data.Count>0)
                {
                    foreach (var item in obj.data)
                    {
                       // var mimi = Encoding.UTF8.GetString( OSharp.Encrypt.SMEncrypt.ECBDecrypt(item.customerName, key));
                        var mimi2 = Encoding.GetEncoding("GBK").GetString( OSharp.Encrypt.SMEncrypt.ECBDecrypt(item.linkPhone, key));
                        var mimi3 = Encoding.GetEncoding("GBK").GetString( OSharp.Encrypt.SMEncrypt.ECBDecrypt(item.detailAddress, key));
                        var mimi4 = Encoding.UTF8.GetString( OSharp.Encrypt.SMEncrypt.ECBDecrypt(item.customerName, key));
                        var phone = OSharp.Encrypt.SMEncrypt.ECBDecrypt(item.linkPhone, key);
                        Console.WriteLine(  mimi);
                        Console.WriteLine(  phone);
                    }                   

                }
              
              
            }
            catch (Exception ex)
            {
                // 捕获并处理异常  
                Console.WriteLine($"发送短信时发生错误: {ex.Message}");

            }
            return null;
        }
         
      
    }
}
